package utils;

import pojo.RuleAutomicParam;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class QueryFromClickHousesqlUtil {


    public static String newSql(String deviceId, RuleAutomicParam ruleAutomicParam) {

        String sql = "select  deviceId , count(*)  as cnt from    event_detail " +
                "where  deviceId='" + deviceId + "' and   eventId ='" + ruleAutomicParam.getEventId() + "'";


        StringBuffer sb = new StringBuffer();
        HashMap<String, String> paramproperties = ruleAutomicParam.getProperties();
        Set<Map.Entry<String, String>> entries = paramproperties.entrySet();
        for (Map.Entry<String, String> entry : entries) {

            sb.append("and properties['" + entry.getKey() + "']='" + entry.getValue() + "'");

        }

        String str3 = "group by  deviceId;";

        return sql + sb.toString() + str3;
    }

    public static void main(String[] args) {
        RuleAutomicParam ruleAutomicParam = new RuleAutomicParam();
        ruleAutomicParam.setEventId("W");
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("p1","v1");
        stringStringHashMap.put("p2","v2");
        ruleAutomicParam.setProperties(stringStringHashMap);

        System.out.println(newSql("0001",ruleAutomicParam));
    }


}
